#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <bitset>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
const int N = 1011;

void get_pos(string filepath, vector<vector<int>> &nums)
{
	fstream ifs;
	ifs.open(filepath, ios::in);
	if (!ifs.is_open())
	{
		cout << "fail" << endl;
	}
	string line;
	for (int i = 0; getline(ifs, line); i++)
	{
		istringstream iss(line); // ��ÿһ������ת��Ϊ������
		int num;
		nums.push_back(vector<int>());
		while (iss >> num)
		{							// �����ȡÿ����
			nums[i].push_back(num); // �洢ÿ����
		}
	}
	ifs.close();
}

void sto_bit(vector<vector<int>> &nums, vector<bitset<N>> &vb)
{
	for (int i = 0; i < nums.size(); i++)
	{
		vb.push_back(bitset<N>());
		for (int j = 0; j < nums[i].size(); j++)
		{
			vb[i][nums[i][j]] = 1;
		}
	}
}

int find_num(vector<int> &v, int num)
{
	for (int i = 0; i < v.size(); i++)
	{
		if (v[i] == num)
		{
			return i;
		}
	}
	return -1;
}

int find_last_1(bitset<N> &b)
{
	for (int i = N - 1; i >= 0; i--)
	{

		if (b[i] == 1)
		{
			return i;
		}
	}
	return -1;
}

int search_all_1(bitset<N> &b, int start)
{
	for (int i = start; i >= 0; i--)
	{
		if (b[i] == 1)
		{
			return i;
		}
	}
	return -1;
}

int main()
{

	string path_eli = "/home/data/Groebner/4_1011_539_263/1.txt";
	string path_be = "/home/data/Groebner/4_1011_539_263/2.txt";

	vector<vector<int>> v_eli;
	vector<vector<int>> v_be;
	vector<bitset<N>> v_eli_bit;
	vector<bitset<N>> v_be_bit;

	vector<bitset<N>> v_res_bit;
	vector<vector<int>> v_res;

	struct timespec sts, ets;
	timespec_get(&sts, TIME_UTC);

	get_pos(path_eli, v_eli);
	sto_bit(v_eli, v_eli_bit);
	get_pos(path_be, v_be);
	sto_bit(v_be, v_be_bit);

	vector<int> first_eli;
	for (int i = 0; i < v_eli.size(); i++)
	{

		first_eli.push_back(v_eli[i][0]);
	}
	vector<int> first_be;
	for (int i = 0; i < v_be.size(); i++)
	{

		first_be.push_back(v_be[i][0]);
	}

	int max_times = first_be.size();
	int be_num = first_be.size();
	int f_id;
	for (int i = 0; i < first_be.size(); i++)
	{
		while (first_be[i] != -1)
		{
			f_id = find_num(first_eli, first_be[i]);
			if (f_id != -1)
			{

				v_be_bit[i] = v_be_bit[i] ^ v_eli_bit[f_id];
				first_be[i] = find_last_1(v_be_bit[i]);
			}
			else
			{
				first_eli.push_back(first_be[i]);
				v_eli_bit.push_back(v_be_bit[i]);
				break;
			}
		}
	}

	int id;
	for (int i = 0; i < v_be_bit.size(); i++)
	{
		id = N;
		v_res.push_back(vector<int>());
		while (1)
		{
			id = search_all_1(v_be_bit[i], id - 1);
			if (id != -1)
			{
				v_res[i].push_back(id);
			}
			else
			{
				break;
			}
		}
	}

	timespec_get(&ets, TIME_UTC);
	time_t dsec = ets.tv_sec - sts.tv_sec;
	long dnsec = ets.tv_nsec - sts.tv_nsec;
	if (dnsec < 0)
	{
		dsec--;
		dnsec += 1000000000ll;
	}
	printf("%lld.%09llds\n", dsec, dnsec);

	system("pause");
	return 0;
}